const { Controller } = require("egg");

class UserController extends Controller {
  async register() {
    const { ctx, service } = this;

    const { account, password, name, workUnit, position, idNumber, type = 'normal', gender, avatar } = ctx.request.body;
    const createUser = await this.service.user.getUser({
      account,
      isDeleted: 0
    });

    let user;
    const params = {
      account,
      password,
      name,
      workUnit,
      position,
      idNumber,
      avatar,
      type,
      gender

    }
    if (createUser && createUser.isDeleted === 1) {
      user = await service.user.updateUser(
        { id: createUser.id },
        { isDeleted: 0, ...params }
      );
    } else {
      user = await service.user.createUser(params);
    }
    const token = await service.auth.create(user);

    this.ctx.helper.success({
      token,
      ...user,
    }, "用户注册成功");


  }

  async delete() {
    const { id } = this.ctx.request.body;
    const user = await this.service.user.getUser({ id, isDeleted: 0 });

    const res = await this.service.user.updateUser({ id: user.id }, { isDeleted: 1 });

    this.ctx.helper.success(res);
  }

  async getUserById() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    const user = await service.user.getUser({ id, isDeleted: 0 });
    this.ctx.helper.success(this.ctx.helper.filterKey(user, "password"));
  }

  async list() {
    const { ctx, service } = this;
    const { pageNum, pageSize, ...others } = ctx.query;
    const query = {
      pageNum: ctx.helper.toInt(pageNum) || 1,
      pageSize: ctx.helper.toInt(pageSize) || 10,
      ...others,
    };
    try {
      const result = await service.user.list(query);
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.success(error);
    }
  }

  async login() {
    const { service, ctx } = this;
    const { account, password, type = 'normal' } = ctx.request.body;
    const accountInfo = await this.app.model.User.findOne({
      where: {
        account,
        type
      },

    });
    if (!accountInfo) {
      if (type === 'admin') {
        return this.ctx.helper.error("管理员账户不存在，请先注册~");
      }
      return this.ctx.helper.error("当前账户不存在，请先注册~");
    }
    if (accountInfo.password !== password) {
      return this.ctx.helper.error("密码错误，请重新输入");
    }

    const user = await service.user.getUser({ id: accountInfo.id });
    const token = await service.auth.create(user);

    this.ctx.helper.success({
      token,
      ...user,
    });
  }

  async loginOut() {

  }

  async updatePassword() {
    const { ctx, service } = this;

    let { id, password } = ctx.request.body;
    const updateUser = await service.user.getUser({ id, isDeleted: 0 });
    if (!updateUser) {
      return this.ctx.helper.error("用户不存在");
    }
    const res = await service.user.updateUser({ id }, { password });
    if (res > 0) {
      const newUser = await service.user.getUser({ id });
      await service.auth.destory(id);
      return this.ctx.helper.success({
        ...newUser,
      });
    } else {
      return this.ctx.helper.error("密码更改失败！");
    }
  }

  async update() {
    const { ctx, service } = this;
    let { id, ...updateFields } = ctx.request.body;
    const existingUser = await service.user.getUser({ id, isDeleted: 0 });
    if (!existingUser) {
      return this.ctx.helper.error("用户不存在");
    }
    const updateResult = await service.user.updateUser({ id }, updateFields);
    return this.ctx.helper.success({
      ...updateResult,
    });

  }

  async checkStatus() {
    const res = await this.service.auth.getCurrentUser();
    return this.ctx.helper.success(res);
  }
}

module.exports = UserController;
